﻿using MTEA.MTTask;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MtApi;
using MTEA.LIB;
// All console commands must be in the sub-namespace Commands:
namespace MTEA.Commands
{
    // Must be a public static class:
    public static class DefaultCommands
    {
        public static string gettasks()
        {
            var sb = new StringBuilder();
            var tasks = MTTaskExecuter.Instance.GetTasks();
            if(tasks.Count()==0)
            {
                return "Empty tasks";
            }
            foreach (var item in tasks)
            {
                sb.AppendLine(ConsoleFormatting.Indent(2) + string.Format("Id:{0} {1}", item.Id, item.Name, item.Description));
                sb.AppendLine(ConsoleFormatting.Indent(2) + item.Description);
            }
            
            return sb.ToString();

        }

        public static string getwatch()
        {
            return string.Format("Execute Watch {0}",MTTaskExecuter.Instance.GetCurrentToken());
        }

        public static string rmtask(int id)
        {
            return string.Format("rmtask id {0} {1}", id,MTTaskExecuter.Instance.RemoveTask(id)?"Successfuly":"Failed");
        }

        public static string closekdj(int timeFrames, double lots, int shift, int KDJType, int KDJCrossType, double maxValue, double minValue)
        {
            var client = MTTaskExecuter.Instance.Client;
            var order = client.GetOrder(0, OrderSelectMode.SELECT_BY_POS, OrderSelectSource.MODE_TRADES);
            Console.WriteLine("Select Order Id {0}", order.Ticket);

            var config = new KDJMTTask.KDJMTTaskConfig()
            {
                TimeFrames = timeFrames,
                Lots = lots,
                OrderType = OrderType.Close,
                TickId = order.Ticket,
                Shift = shift,
                KDJType = KDJType,
                CrossType = (LIB.Analysis.KDJ.KDJCrossType)KDJCrossType,
                MaxValue = maxValue,
                MinValue = minValue
            };
            var task = new KDJMTTask(config);
            var id = MTTaskExecuter.Instance.AddTask(task);
            var result = string.Format(ConsoleFormatting.Indent(2) +
                "AddTask Successfully new Id {0}", id);
            return result;

        }

        public static string addkdjtask(int timeFrames, double lots, int type, int shift, int KDJType, int KDJCrossType, double maxValue,double minValue,int tickId = -1)
        {
            var config = new KDJMTTask.KDJMTTaskConfig()
            {
                TimeFrames = timeFrames,
                Lots = lots,
                OrderType = (OrderType)type,
                TickId = tickId,
                Shift = shift,
                KDJType = KDJType,
                CrossType=(LIB.Analysis.KDJ.KDJCrossType)KDJCrossType,
                MaxValue=maxValue,
                MinValue=minValue
            };
            var task = new KDJMTTask(config);
            var id = MTTaskExecuter.Instance.AddTask(task);
            var result = string.Format(ConsoleFormatting.Indent(2) +
                "AddTask Successfully new Id {0}", id);
            return result;

        }
        public static string addtask(int timeFrames, double lots, int type, double duration, int bollingType, int tickId = -1)
        {
            var config = new BollingMTTask.BollingMTTaskConfig()
            {
                TimeFrames = timeFrames,
                Lots = lots,
                OrderType = (OrderType)type,
                Duration = duration,
                BollingType = bollingType,
                TickId = tickId
            };

            var task = new BollingMTTask(config);
            var id = MTTaskExecuter.Instance.AddTask(task);
            var result = string.Format(ConsoleFormatting.Indent(2) +
                "AddTask Successfully new Id {0}", id);

            return result;
        }

        public static string closeorder(int timeFrames,double lots, double duration, int bollingType)
        {
            var client = MTTaskExecuter.Instance.Client;
            var order=client.GetOrder(0, OrderSelectMode.SELECT_BY_POS, OrderSelectSource.MODE_TRADES);
            Console.WriteLine("Select Order Id {0}",order.Ticket);
            var config = new BollingMTTask.BollingMTTaskConfig()
            {
                TimeFrames = timeFrames,
                Lots = lots,
                OrderType = OrderType.Close,
                Duration = duration,
                BollingType = bollingType,
                TickId = order.Ticket
            };

            var task = new BollingMTTask(config);
            var id = MTTaskExecuter.Instance.AddTask(task);
            var result = string.Format(ConsoleFormatting.Indent(2) +
                "AddTask Successfully new Id {0}", id);

            return result;
        }

        public static string exit()
        {
            Environment.Exit(0);
            return "exit";
        }

    }
}
